package com.the4man.cnbdw.service.impl;

import com.the4man.cnbdw.dao.EmpHeartrateInfoMapper;
import com.the4man.cnbdw.exception.BusinessException;
import com.the4man.cnbdw.service.EmpHeartrateInfoService;
import com.the4man.framework.model.EmpHeartrateInfo;
import com.the4man.framework.model.EmpHeartrateInfoExample;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by lenovo on 2017/12/12.
 */
@Service
public class EmpHeartrateInfoServiceImpl implements EmpHeartrateInfoService {
    @Autowired
    private EmpHeartrateInfoMapper empHeartrateInfoMapper;

    public EmpHeartrateInfoServiceImpl() {
    }

    public int getHeartrateInfoCnt(String empId, Date start, Date end) throws BusinessException, Exception {
        EmpHeartrateInfoExample example = new EmpHeartrateInfoExample();
        EmpHeartrateInfoExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotEmpty(empId) || !empId.equals("")) {
            criteria.andEmpIdEqualTo(empId);
        }

        if(start != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(start);
        }

        if(end != null) {
            criteria.andCreateTimeLessThanOrEqualTo(end);
        }

        int count = this.empHeartrateInfoMapper.countByExample(example);
        return count;
    }

    public List<EmpHeartrateInfo> getHeartrateInfoPage(int startRows, int pageSize, String empId, Date start, Date end) throws BusinessException, Exception {
        EmpHeartrateInfoExample example = new EmpHeartrateInfoExample();
        example.setStartRow(startRows);
        example.setPageSize(pageSize);
        EmpHeartrateInfoExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotEmpty(empId) || !empId.equals("")) {
            criteria.andEmpIdEqualTo(empId);
        }

        if(start != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(start);
        }

        if(end != null) {
            criteria.andCreateTimeLessThanOrEqualTo(end);
        }

        example.setOrderByClause("create_time desc");
        List<EmpHeartrateInfo> list = this.empHeartrateInfoMapper.selectByExample(example);
        return list;
    }

    public boolean importtHeartrateInfo(List<EmpHeartrateInfo> importList) throws BusinessException, Exception {
        int count = 0;
        Iterator var4 = importList.iterator();

        while(var4.hasNext()) {
            EmpHeartrateInfo empHeartrateInfo = (EmpHeartrateInfo)var4.next();
            System.out.println(JSONObject.fromObject(empHeartrateInfo));
            int rows = this.empHeartrateInfoMapper.insert(empHeartrateInfo);
            if(rows > 0) {
                ++count;
            }
        }

        if(count > 0) {
            return true;
        } else {
            return false;
        }
    }

    public List<EmpHeartrateInfo> stepRankingList() throws BusinessException, Exception {
        List<EmpHeartrateInfo> list = this.empHeartrateInfoMapper.stepRankingList();
        return list;
    }
}
